package main.Web;

import com.alibaba.fastjson.JSON;
import main.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者: ALIM-MASTIK
 * 创建实现: 2023/7/18 10:47
 * 项目名称：chat
 * 软件: IntelliJ IDEA
 */
@WebServlet("/Chatserver")
public class Chatserver extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String,Object> map = new HashMap<>();
        resp.setHeader("Access-Control-Allow-Origin", "*");
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        List<User> Friends=(List<User>)req.getSession().getAttribute("Friends");
        for(User user: Friends){
//            System.out.println(user.toString());
//            System.out.println(user.getUserID());
//            System.out.println(((User)req.getSession().getAttribute("user")).getUserID());
//            JdbcUtils.saveUserAvatarToDesktopTemp(getConnection(), user.getUserID());
        }
        map.put("Friends",Friends);
        String json = JSON.toJSONString(map);
        PrintWriter pw = resp.getWriter();
        pw.print(json);
    }
}
